package com.asd.map.controller;

import com.alibaba.fastjson.JSONArray;
import com.asd.common.Result;
import com.asd.map.entity.Line_style;
import com.asd.map.entity.Point_style;
import com.asd.map.entity.vo.Point_styleVO;
import com.asd.map.service.IPoint_styleService;
import com.asd.system.entity.Icon;
import com.asd.system.service.IIconService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author
 * @createtime 2021-10-15 9:44
 * @description
 */
@RestController
@RequestMapping("/api/point_style")
@Api(tags = "点位样式Api")
public class Point_styleController {

    @Autowired
    private IPoint_styleService point_styleService;

    @Autowired
    private IIconService iconService;


//    /**
//     * 编辑
//     *
//     * @param
//     * @return
//     */
//    @ApiOperation(value = "点位样式-编辑", notes = "点位样式-编辑")
//    @PutMapping(value = "/update")
//    public Result<?> edit(@RequestBody Point_style point_style) {
//
//        String id = point_style.getId();
//        if (id == null || id.equals("")) {//不存在点位样式时就新增
//
//            point_styleService.save(point_style);
//
//            return Result.ok(point_style);
//        } else {
//            //存在点位样式时
//            Point_style old_point_style = point_styleService.getById(id);
//
//            old_point_style.setType(point_style.getType());
//            String link = point_style.getLink();
//            if (link.equals(null) || link.equals("")) {//非自定义图标时
//                if (!(point_style.getColor().equals(null) || point_style.getColor().equals(""))) {
//
//                    old_point_style.setColor(point_style.getColor());
//                }
//                if (!(point_style.getSize().equals(null) || point_style.getSize().equals(""))) {
//                    old_point_style.setSize(point_style.getSize());
//                }
//                if (!(old_point_style.getLink().equals(null) || old_point_style.getLink().equals(""))) {
//                    old_point_style.setLink("");
//                }
//
//                point_styleService.updateById(old_point_style);
//
//            } else {
//                if (point_style.getSize().isEmpty() && point_style.getColor().isEmpty()) {//自定义图标时
//                    if (!(old_point_style.getColor().equals(null) || old_point_style.getColor().equals(""))) {
//
//                        old_point_style.setColor("");
//                    }
//                    if (!(old_point_style.getSize().equals(null) || old_point_style.getSize().equals(""))) {
//                        old_point_style.setSize("");
//                    }
//                    old_point_style.setLink(link);
//                    point_styleService.updateById(old_point_style);
//                } else {//栅格图标时
//                    old_point_style.setSize(point_style.getSize());
//                    old_point_style.setColor(point_style.getColor());
//                    old_point_style.setLink(point_style.getLink());
//                    point_styleService.updateById(old_point_style);
//                }
//            }
//
//            return Result.ok(old_point_style);
//        }
//    }

    /**
     * 编辑
     *
     * @param
     * @return
     */
    @ApiOperation(value = "点位样式-编辑", notes = "点位样式-编辑")
    @PutMapping(value = "/update")
    public Result<?> edit1(@RequestBody Point_style point_style) {


        List<Point_style> styleList = point_styleService.findByApplyId(point_style.getApplyId(), point_style.getDid());


        if (styleList.size() == 0) {//不存在点位样式时就新增

            point_styleService.save(point_style);

            return Result.ok(point_style);
        } else {

//            for (Point_style point_style1 : styleList) {
            for (int i = 0; i < styleList.size(); i++) {
                Point_style point_style1 = styleList.get(i);
                if (point_style1.getType().equals("0")) {//存在样式为统一样式时

                    Point_style old_point_style = point_styleService.getById(point_style1.getId());
                    old_point_style.setType(point_style.getType());
                    String link = point_style.getLink();
                    if (link.equals(null) || link.equals("")) {//非自定义图标时
                        if (!(point_style.getColor().equals(null) || point_style.getColor().equals(""))) {

                            old_point_style.setColor(point_style.getColor());
                        }
                        if (!(point_style.getSize().equals(null) || point_style.getSize().equals(""))) {
                            old_point_style.setSize(point_style.getSize());
                        }
                        if (!(old_point_style.getLink().equals(null) || old_point_style.getLink().equals(""))) {
                            old_point_style.setLink("");
                        }

                        point_styleService.updateById(old_point_style);

                    } else {
                        if (point_style.getSize().isEmpty() && point_style.getColor().isEmpty()) {//自定义图标时
                            if (!(old_point_style.getColor().equals(null) || old_point_style.getColor().equals(""))) {

                                old_point_style.setColor("");
                            }
                            if (!(old_point_style.getSize().equals(null) || old_point_style.getSize().equals(""))) {
                                old_point_style.setSize("");
                            }
                            old_point_style.setLink(link);
                            point_styleService.updateById(old_point_style);
                        } else {//栅格图标时
                            old_point_style.setSize(point_style.getSize());
                            old_point_style.setColor(point_style.getColor());
                            old_point_style.setLink(point_style.getLink());
                            point_styleService.updateById(old_point_style);
                        }
                    }

                    return Result.ok(old_point_style);

                } else {//存在样式不为为统一样式时

                    point_styleService.removeById(point_style1.getId());
                    if (i == styleList.size() - 1) {
                        point_styleService.save(point_style);
                    }
                }

            }
            return Result.ok(point_style);

        }
    }


    /**
     * 分类样式
     *
     * @param
     * @return
     */
    @ApiOperation(value = "点位样式-分类样式", notes = "点位样式-分类样式")
    @PutMapping(value = "/updateStyleTriage")
    public Result<?> updateStyleTriage(@RequestBody Point_styleVO point_styleVO) {


        List<Point_style> styleList = point_styleService.findByApplyId(point_styleVO.getApplyId(), point_styleVO.getDid());

        List<Point_style> list = new ArrayList<>();

        if (styleList.size() > 0) {
            for (Point_style pointStyle : styleList) {

                point_styleService.removeById(pointStyle.getId());
            }

        }

        String data = point_styleVO.getData();


        List<Map<String, String>> dataList = (List<Map<String, String>>) JSONArray.parse(data);
        for (int i = 0; i < dataList.size(); i++) {
            Map<String, String> map = dataList.get(i);
            String pid = map.get("pid");
            String size = map.get("size");
            String color = map.get("color");

            Point_style point_style = new Point_style();
            point_style.setApplyId(point_styleVO.getApplyId());
            point_style.setDid(point_styleVO.getDid());
            point_style.setPid(pid);
            point_style.setType(point_styleVO.getType());
            point_style.setSize(size);
            point_style.setColor(color);
            point_styleService.save(point_style);
            list.add(point_style);
        }

        return Result.ok(list);

    }

    /**
     * 分类样式-单点样式修改
     *
     * @param
     * @return
     */
    @ApiOperation(value = "点位样式-分类样式-根据id修改点样式", notes = "点位样式-分类样式-根据id修改点样式")
    @PutMapping(value = "/updateStyleById")
    public Result<?> updateStyleById(@ApiParam(name = "id", value = "样式id", required = true) @RequestParam("id") String id,
                                     @ApiParam(name = "size", value = "大小") @RequestParam("size") String size,
                                     @ApiParam(name = "color", value = "颜色") @RequestParam("color") String color) {

        Result<?> result = point_styleService.updateStyleById(id, size, color);

        return result;
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "点位样式-通过id删除", notes = "点位样式-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {

        point_styleService.removeById(id);


        return Result.ok("删除成功!");
    }

    /**
     * 通过id删除
     *
     * @param
     * @return
     */
    @ApiOperation(value = "点位样式-删除数据源关联样式", notes = "点位样式-通过id删除")
    @DeleteMapping(value = "/deleteForInformation")
    public Result<?> delete1(@RequestParam(name = "applyId", required = true) String applyId, @RequestParam(name = "did", required = true) String did) {

        point_styleService.deleteById(applyId, did);


        return Result.ok("删除成功!");
    }


    /**
     * 自定义图标
     *
     * @param fileimage
     * @return
     * @throws IOException
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileimage", value = "自定义图标", required = true),
    })
    @ApiOperation(value = "点位样式-自定义图标", notes = "自定义图标")
    @PostMapping("/file")
    public Result<?> uploadFile(@RequestParam("fileimage") MultipartFile fileimage,
                                @RequestParam("userName") String userName) throws IOException {


//        final String systemPath = "http://192.168.124.10:8080/images/";//本地回显地址前缀
//        final String imagePathRoot = "E:\\upload\\images\\";//存放图片的本地路径

        final String systemPath = "http://dtxt.iansn.com/iconPictures/";//服务器回显地址前缀
        final String imagePathRoot = "/www/wwwroot/dtxt.iansn.com/iconPictures/";//存放图片的服务器路径


        File file = new File(imagePathRoot);
        if (!file.exists()) {
            file.mkdirs();
        }

        if (fileimage.isEmpty()) {
            return Result.error("请选择文件！！！");
        }
        //获取原文件名
        String fileName = fileimage.getOriginalFilename();
        //获取后缀名
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        //获取UUID为新的图片进行重新赋值（防止重复）
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String imageFilePath = imagePathRoot + uuid + fileType;
        fileimage.transferTo(new File(imageFilePath));



        String echoPath = systemPath + uuid + fileType;



        String path = imageFilePath;

        Icon icon = new Icon();

        icon.setName(fileName);
        icon.setPath(path);
        icon.setUserName(userName);

        icon.setIconEchoPath(echoPath);
        iconService.save(icon);

        return Result.ok(icon);
    }

    /**
     * 自定义图标删除
     *
     * @param iconEchoPath
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iconEchoPath", value = "图标回显地址", required = true),
    })
    @ApiOperation(value = "点位样式-自定义图标删除", notes = "点位样式-自定义图标删除")
    @DeleteMapping("/deletePrimary")
    public Result<?> deletePrimaryImage(@RequestParam("iconEchoPath") String iconEchoPath) {


        final String imagePathRoot = "/www/wwwroot/dtxt.iansn.com/iconPictures/";//存放图片的服务器路径
//        final String imagePathRoot = "E:\\upload\\images\\";//存放图片的本地路径

        //获取服务器中存储的图片名称
        String fiName = iconEchoPath.substring(iconEchoPath.lastIndexOf("/"));

        File file = new File(imagePathRoot + fiName);
        try {
            boolean flag = file.delete(); // 删除存放路径中的图标
            iconService.deletByIconEchoPath(iconEchoPath);//删除自定义图标表中的数据

            if (flag) {
                return Result.ok("删除成功");
            } else {
                return Result.ok("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("删除失败");
        }
    }

    /**
     * 获取自定义图标列表
     *
     * @param
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "用户名", required = true),
    })
    @ApiOperation(value = "点位样式-获取自定义图标列表", notes = "点位样式-获取自定义图标列表")
    @GetMapping("/iconList")
    public Result<?> selectByUserName(@RequestParam("userName") String userName) {

        List<String> linkList = iconService.selectByUserName(userName);

        return Result.ok(linkList);
    }


}
